﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using Nexxt.Framework;
using Nexxt.Engine.GameObjects;

namespace Nexxt.Engine.TypeReaders
{
    public class MapTypeReader : ContentTypeReader<Map>
    {
        protected override Map Read(ContentReader input, Map existingInstance)
        {
            //then we can read in all the textures
            int tCount = input.ReadInt32();
            List<Texture2D> textures = new List<Texture2D>();
            for (int i = 0; i < tCount; i++)
                textures.Add(input.ReadExternalReference<Texture2D>());

            //next the background texture
            Texture2D background = input.ReadExternalReference<Texture2D>();

            List<GameObject> objectDataBase = new List<GameObject>();
            //Reads all the sprites for the game
            tCount = input.ReadInt32();

            for (int i = 0; i < tCount; i++)
            {
                objectDataBase.Add(input.ReadObject<GameObject>());
            }

            //and finally the map layout
            int rCount = input.ReadInt32();
            List<int[]> rows = new List<int[]>();
            for (int i = 0; i < rCount; i++)
                rows.Add(input.ReadObject<int[]>());

            //now we cram that map layout into the proper int[,] structure
            int[,] map = new int[rCount, rows[0].Length];
            for (int i = 0; i < rCount; i++)
                for (int j = 0; j < rows[i].Length; j++)
                    map[i, j] = rows[i][j];

            //and return the new map object
            return new Map(textures, background, map, objectDataBase);
        }
    }
}
